package com.eveinfo.api.character.skilltraining;

import java.text.ParseException;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.eveinfo.api.AbstractApiHandler;
import com.eveinfo.api.ApiError;

public class SkillTrainingHandler extends AbstractApiHandler<ApiSkillTraining> {

	public SkillTrainingHandler() {
		super();
		response = new ApiSkillTraining();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		super.endElement(uri, localName, qName);
		String value = (!localName.equals("")) ? localName : qName;
		try {
			if (value.equals("trainingEndTime")) {
				response.setTrainingEndTime(sdf.parse(text));
			} else if (value.equals("trainingStartTime")) {
				response.setTrainingStartTime(sdf.parse(text));
			} else if (value.equals("trainingTypeID")) {
				response.setTrainingTypeID(Integer.parseInt(text));
			} else if (value.equals("trainingStartSP")) {
				response.setTrainingStartSP(Integer.parseInt(text));
			} else if (value.equals("trainingDestinationSP")) {
				response.setTrainingDestinationSP(Integer.parseInt(text));
			} else if (value.equals("trainingToLevel")) {
				response.setTrainingToLevel(Integer.parseInt(text));
			} else if (value.equals("skillInTraining")) {
				if (Integer.parseInt(text) > 0) {
					response.setSkillInTraining(true);
				} else {
					response.setSkillInTraining(false);
				}
			}
		} catch (ParseException e) {
			error = new ApiError();
			error.setError(e.getMessage());
			e.printStackTrace();
		}
	}
}
